package controller

import (
	"common/zapx"
	"context"
	"email/internal/logic"
	"email/internal/pb"
	"errors"
	"go.uber.org/zap"
)

type EmailService struct {
	pb.UnimplementedEmailServer
}

// SendVerifyEmail 发送验证码控制器
func (e *EmailService) SendVerifyEmail(ctx context.Context, req *pb.SendVerifyEmailRequest) (*pb.SendVerifyEmailResponse, error) {
	if err := logic.SendVerifyCode(req.Email, req.Event); err != nil {
		zapx.Logger.Error("发送邮件失败了", zap.Error(err))
		return nil, errors.New("发送失败")
	}
	return &pb.SendVerifyEmailResponse{}, nil
}

// VerifyCode 验证邮件验证码
func (e *EmailService) VerifyCode(ctx context.Context, req *pb.VerifyCodeRequest) (*pb.VerifyCodeResponse, error) {
	if err := logic.VerifyCode(req.Email, req.Code, req.Event); err != nil {
		zapx.Logger.Error("验证邮件失败了", zap.Error(err))
		return nil, errors.New("验证失败")
	}
	return &pb.VerifyCodeResponse{}, nil
}

// CheckCode  检查验证码是否有效
func (e *EmailService) CheckCode(ctx context.Context, req *pb.CheckCodeRequest) (*pb.CheckCodeResponse, error) {
	if err := logic.CheckCode(req.Email, req.Code, req.Event); err != nil {
		return nil, errors.New(err.Error())
	}
	return &pb.CheckCodeResponse{}, nil
}

// UseCode 使用验证码
func (e *EmailService) UseCode(ctx context.Context, req *pb.UseCodeRequest) (*pb.UseCodeResponse, error) {
	if err := logic.UseCode(req.Email, req.Code, req.Event); err != nil {
		return nil, err
	}
	return &pb.UseCodeResponse{}, nil
}
